package com.dxx.D201910312055.dxx.utils;

import android.view.accessibility.AccessibilityNodeInfo;

import static java.lang.System.out;

//单控件查找的
public class FindAccessibilityNodeInfosByClassName {

    /**
     * 前面查找
     * 包装方法，不用isChind = 0可以直接使用
     *
     * @param nodeInfo  父级控件
     * @param ClassName 查找的控件名字
     * @param Chind     跳过的数量
     * @return 返回查找并跳过的控件
     */
    public AccessibilityNodeInfo frontFindAccessibilityNodeInfosByClassName(final AccessibilityNodeInfo nodeInfo, final String ClassName, int Chind) {
        return new newClass().frontFindAccessibilityNodeInfosByClassName(nodeInfo, ClassName, Chind);
    }

    /**
     * 后面查找
     * 包装方法，，不用isChind = 0可以直接使用
     *
     * @param nodeInfo  父级控件
     * @param ClassName 查找的控件名字
     * @param Chind     跳过的数量
     * @return 返回查找并跳过的控件
     */
    public AccessibilityNodeInfo behindFindAccessibilityNodeInfosByClassName(final AccessibilityNodeInfo nodeInfo, final String ClassName, int Chind) {
        return new newClass().behindFindAccessibilityNodeInfosByClassName(nodeInfo, ClassName, Chind);
    }


    //    每次都通过new使用的方法
    class newClass {
        private AccessibilityNodeInfo isAccessibilityNodeInfo = null;
        private int isChind = 0;

        public newClass() {
            isChind = 0;
        }

        /**
         * 从nodeInfo父级 前面查找 ClassNamet名字的nodeInfo,并选择第Chind个返回
         * 每次使用之前需要确保 isChind = 0 不然逻辑错误,请通过new 调用
         *
         * @param nodeInfo  nodeInfo 父级
         * @param ClassName ClassNamet 控件名字
         * @param Chind     跳过Chind次查找到的控件
         * @return 返回查找到并跳过Chind次的控件
         */
        private AccessibilityNodeInfo frontFindAccessibilityNodeInfosByClassName(final AccessibilityNodeInfo nodeInfo, final String ClassName, int Chind) {


            isAccessibilityNodeInfo = null;
            if (nodeInfo == null) {
                isAccessibilityNodeInfo = null;
                isChind = 0;
                return isAccessibilityNodeInfo;
            }
            int childCount = nodeInfo.getChildCount();


            if (ClassName.equals(nodeInfo.getClassName())) {
                isChind++;
                if (isChind > Chind) {
                    //因为是递规，经管可能最后不是返回这个，但是这里还是要中断的
//                out.println(nodeInfo.getClassName());
                    nodeInfo.refresh();
//成功赋值
                    isAccessibilityNodeInfo = nodeInfo;
                    isChind = 0;
                    return isAccessibilityNodeInfo;
                }


            }


            //递归在循环中，返回递归会中断。但是循环并不会中断
            for (int i = 0; i < childCount; i++) {
                if (isAccessibilityNodeInfo != null) {
//                    说明成功了
                    break;
                }
                AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
                frontFindAccessibilityNodeInfosByClassName(childNodeInfo, ClassName, Chind);

            }


            return isAccessibilityNodeInfo;
        }

        private AccessibilityNodeInfo behindFindAccessibilityNodeInfosByClassName(final AccessibilityNodeInfo nodeInfo, final String ClassName, int Chind) {

            isAccessibilityNodeInfo = null;
            if (nodeInfo == null) {
                isAccessibilityNodeInfo = null;
                isChind = 0;
                return isAccessibilityNodeInfo;
            }
            int childCount = nodeInfo.getChildCount();


            if (ClassName.equals(nodeInfo.getClassName())) {
                isChind++;
                if (isChind > Chind) {
                    //因为是递规，经管可能最后不是返回这个，但是这里还是要中断的
//                out.println(nodeInfo.getClassName());
                    nodeInfo.refresh();
//成功赋值
                    isAccessibilityNodeInfo = nodeInfo;
                    isChind = 0;
                    return isAccessibilityNodeInfo;
                }


            }


            //遍历
            for (int i = 1; i < childCount + 1; i++) {
                if (isAccessibilityNodeInfo != null) {
//                说明成功了，就退出循环
                    break;
                }

                AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(childCount - i);
                behindFindAccessibilityNodeInfosByClassName(childNodeInfo, ClassName, Chind);


            }


            return isAccessibilityNodeInfo;
        }


    }


}
